//
//  JCLEmoticonPackage.swift
//  JCLEmoticons
//
//  Created by DDC on 2017/9/25.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit
import YYModel

class JCLEmoticonPackage: NSObject {
    
    var groupName:String?
    
    var directory:String? {
        didSet {
            // 当设置目录时,从目录中加载info.plist文件
            guard
            let directory = directory ,
            let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil),
            let bundle = Bundle(path: path),
            let infoPath = bundle.path(forResource: "info.plist", ofType: nil, inDirectory: directory),
            let array = NSArray(contentsOfFile: infoPath),
            let models = NSArray.yy_modelArray(with: JCLEmoticon.self, json:array) as? [JCLEmoticon]
                
                else {
                return
            }
       
            // 为每一个模型设置路径
            for m in models {
            
                m.directory = directory
            }
            emoticons += models
            
            
        }
    }
    // 表情数组模型
    lazy var emoticons = [JCLEmoticon]()
    
    // 表情页面数量
    var numberOfPages :Int {
        return (emoticons.count - 1) / 20 + 1
    }
    
    // 根据页面来设置表情键盘的模型数组
    // 假如有26个
    // page:0 -- 数组(0~19)
    // page:1 -- 数组(20~25)
    func emotions(page:Int) -> [JCLEmoticon] {
        let count = 20
        let location = page * 20
        var length = count
        
        // 越界
        if (location + length) > emoticons.count {
            length = emoticons.count - location
        }
        
        let range = NSRange(location: location , length: length)
        let subArray = (emoticons as NSArray).subarray(with: range)
        return subArray as! [JCLEmoticon]
    }
    
    override var description: String {
        
        return yy_modelDescription()
    }


}
